<?php

/**
 * This is the model class for table "virtual".
 *
 * The followings are the available columns in table 'virtual':
 * @property string $story
 * @property string $event
 * @property string $time
 * @property string $scene
 * @property string $role
 * @property string $object
 * @property string $pedia
 * @property string $tag
 */
class Virtual extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Virtual the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'virtual';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('story, event, time, scene, role, object, pedia, tag', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('story, event, time, scene, role, object, pedia, tag', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'story' => '故事',
			'event' => '事件',
			'time' => '时间',
			'scene' => '地点',
			'role' => '角色',
			'object' => '物品',
			'pedia' => '知识',
			'tag' => '标签',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('story',$this->story,true);
		$criteria->compare('event',$this->event,true);
		$criteria->compare('time',$this->time,true);
		$criteria->compare('scene',$this->scene,true);
		$criteria->compare('role',$this->role,true);
		$criteria->compare('object',$this->object,true);
		$criteria->compare('pedia',$this->pedia,true);
		$criteria->compare('tag',$this->tag,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 数据验证后、保存前的处理
	 * 对用户提交的tags中可能的不规范分割符进行替换
	 */
	protected  function afterValidate()
	{
		// 规范表单提交的数据
		$this->tag = str_replace(array("，", " "), ",",$this->tag);
		$this->scene = str_replace(array("，", " "), ",",$this->scene);
		$this->role = str_replace(array("，", " "), ",",$this->role);
		$this->object = str_replace(array("，", " "), ",",$this->object);
		$this->pedia = str_replace(array("，", " "), ",",$this->pedia);
	}
}